
<!DOCTYPE html>
<html>
<head>
<?php $this->inc('_pub/weys~imp'); ?>
<script src="https://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<title>扫码接口 - <?=$_cbase['name']?></title>
<style type="text/css">
nav{ padding:1rem; background:#FFF; }
</style>

</head>
<body>

<header>
    <a href="<?=path('0')?>" target='_self' class="fa fa-home"></a>
    <h1>扫码接口</h1>
</header>

<article>

<p class="tc" id='resRow' style="display:none;">
    扫码结果 <br>
    <b id="resScan">xxx</b>
</p>

<?php if($isWxmob){ ?>
<nav class="tc">
    <a class="btn" onclick="doScan()">开始扫码</a>
</nav>
<?php }else{ ?>
<nav class="tc">
    <a class="btn btn-gray">请用 `微信` 或 `企业微信` 客户端扫码</a>
</nav>
<?php } ?>

</article>

<?php $this->inc('_pub/weys~foot'); ?>

<!-- 
第一步: 在服务端生成签名(Ctrl)
第二步: 引用JSAPI 的脚本文件 
https://res.wx.qq.com/wwopen/js/jsapi/jweixin-1.0.0.js
http://res.wx.qq.com/open/js/jweixin-1.2.0.js
-->
<script src="https://res.wx.qq.com/wwopen/js/jsapi/jweixin-1.0.0.js"></script>
<script>
/*
* 第三步: 配置jsapi的权限 
* 注意：所有的JS接口只能可信域名下调用   
*/

var jsApiList = [
    'checkJsApi',
    'onMenuShareAppMessage',
    'onMenuShareWechat',
    'onMenuShareTimeline',
    'shareAppMessage',
    'shareWechatMessage',
    'chooseImage',
    'previewImage',
    'uploadImage',
    'downloadImage',
    'getNetworkType',
    'openLocation',
    'getLocation',
    'hideOptionMenu',
    'showOptionMenu',
    'hideMenuItems',
    'showMenuItems',
    'hideAllNonBaseMenuItem',
    'showAllNonBaseMenuItem',
    'closeWindow',
    'scanQRCode',
    'previewFile',
    'openEnterpriseChat',
    'selectEnterpriseContact',
    'onHistoryBack',
    'openDefaultBrowser'
    // 所有要调用的 API 都要加到这个列表中
];

function wewLog(key, res){
  alert(key+':::'+JSON.stringify(res));
  console.log(res); // 回调
}

wx.config({
  debug: <?php echo $debug?'true':'false'; ?> ,
  beta: true, 
  appId: '<?php echo $signPackage["corpId"];?>',    //此处的appId等同于企业的CorpID
  timestamp: <?php echo $signPackage["timestamp"];?>,
  nonceStr: '<?php echo $signPackage["nonceStr"];?>',
  signature: '<?php echo $signPackage["signature"];?>',
  /*success: function(res) {
      wewLog('config:success:', res);
  },*/
  jsApiList: jsApiList
}); 

//wx.ready(function(){});

wx.error(function (res){
    wewLog('wx.error', res);
});

function cbScan(sres){
    var view = '<?=$view?>', redir = '<?=$redir?>';
    if(redir!='' && redir.indexOf('://'>0)){
        window.location.href = redir + sres;
    }else if(view=='dir' && sres.indexOf('://'>0)){
        window.location.href = sres;
    }else{
        $('#resScan').text(sres);
        $('#resRow').show();
    }
}

function doScan(){
    wx.scanQRCode({
      needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
      scanType: ["qrCode","barCode"], // 可以指定扫二维码还是一维码，默认二者都有
      success: function (res) {
        var result = res.resultStr; // 当needResult 为 1 时，扫码返回的结果
        cbScan(result);
      }
    }); 
}

<?php if($isWxmob && !$debug){ echo "wx.ready(function(){doScan()});"; }?>
// setTimeout(function(){doScan()},570);

</script>

</body>
</html>
